/**
 * 
 */
package com.dingding.service.user.core.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.base.daox.ISQLBaseDao;
import com.dingding.common.core.beans.UserLogin;
import com.dingding.common.core.util.DateFormatUtil;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facade.user.dto.evaluate.GetDriverInfoDTO;
import com.dingding.facade.user.dto.userinfo.ResUserInfoByCancelOrderDTO;
import com.dingding.service.user.core.dao.IUserInfoDao;
import com.dingding.service.user.core.sql.UserInfoSQL;
import com.dingding.service.user.core.sql.UserLoginSQL;

/**
 * @ClassName: UserInfoDaoImpl
 * @Description:用户信息操作dao
 * @author zhouxuan
 * @date 2016年4月12日 下午4:03:55
 */
@Component("userInfoDao")
public class UserInfoDaoImpl extends BaseDaoImpl<UserLogin> implements IUserInfoDao {
	private static final Logger log = Logger.getLogger(UserInfoDaoImpl.class);

	@Resource(name = "SQLBaseDao")
	private ISQLBaseDao sqlBaseDao;
	
	/**
	 * @Title: getUserCouponNumber
	 * @Description:根据用户id获取优惠券数目
	 * @param @param
	 *            userId
	 * @param @return
	 *            优惠券数目
	 * @author zhouxuan
	 */
	@Transactional(readOnly = true)
	public int getUserCouponNumber(String userId) {
		UserLogin userLogin = null;
		Criterion criterion = Restrictions.eq("uid", userId);
		try {
			userLogin = this.queryCriteriaUnResult(criterion);
		} catch (Exception e) {
			log.error("获取用户优惠券数目失败！UserInfoDaoImpl:getUserCouponNumber()", e);
		}
		if (userLogin != null) {
			return userLogin.getCouponNumber();
		}
		return 0;
	}

	/**
	 * @Title: setUserCouponNumber
	 * @Description: 设置用户优惠券数量
	 * @param @param
	 *            userId 用户id
	 * @param @param
	 *            couponNumber 优惠券数量
	 * @author zhouxuan
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean setUserCouponNumber(String userId, int couponNumber) {
		UserLogin userLogin = null;
		Criterion criterion = Restrictions.eq("uid", userId);
		try {
			userLogin = this.queryCriteriaUnResult(criterion);
		} catch (Exception e) {
			log.error("设置用户优惠券数目失败！UserInfoDaoImpl:setUserCouponNumber()", e);
		}
		if (userLogin != null) {
			userLogin.setCouponNumber(couponNumber);
			return true;
		}
		return false;
	}

	/**
	 * @Title: setUserCouponNumber
	 * @Description: 增加一张用户优惠券数目
	 * @param @param
	 *            userId 用户id
	 * @author zhouxuan
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean addUserCoupon(String userId) {
		UserLogin userLogin = null;
		Criterion criterion = Restrictions.eq("uid", userId);
		try {
			userLogin = this.queryCriteriaUnResult(criterion);
		} catch (Exception e) {
			log.error("设置用户优惠券数目失败！UserInfoDaoImpl:addUserCoupon()", e);
		}
		if (userLogin != null) {
			userLogin.setCouponNumber(userLogin.getCouponNumber() + 1);
			return true;
		}
		return false;

	}

	/**
	 * 
	 * 订单完成后，乘客评价页面需要显示车主信息及部分订单信息，当前接口由此产生
	 * 
	 * 由于当前接口所需要的车主信息需要依赖顶大信息所以获取订单信息也在此接口完成
	 * 
	 * @Title: getDriverInfo_And_OrderInfo
	 * @param orderId
	 *            订单ID
	 * @author CHEHUANBO
	 * @return GetDriverInfoDTO 获取到的车主信息及部分相关订单信息
	 * 
	 */
	@Override
	@Transactional(readOnly=true)
	public GetDriverInfoDTO getDriverInfo_And_OrderInfo(String orderId,Integer registrationType) {

		List<Object[]> objects = new ArrayList<Object[]>();
		try {
			
			if(registrationType == 2){
				objects = super.listSqlObj(UserLoginSQL.GET_TAXI_DRIVERINFO_AND_ORDERINFO, orderId);
			}else if(registrationType == 1){
				objects = super.listSqlObj(UserLoginSQL.GET_DRIVERINFO_AND_ORDERINFO, orderId);
			}else if(registrationType == 3 || registrationType == 4){
				objects = super.listSqlObj(UserLoginSQL.GET_SPECIAL_DRIVERINFO_AND_ORDERINFO, orderId);
			}
			
		} catch (Exception e) {
			log.error("用户评价获取车主信息出现异常！",e);
		}

		if (objects != null && objects.size() > 0) {
			
			GetDriverInfoDTO driverInfoDTO = new GetDriverInfoDTO();
			Object[] obj = objects.get(0);
			
			if (obj[0] != null) { // 下单时间
				driverInfoDTO.setCreateTime(DateFormatUtil.getMMddEHHss(obj[0]));
			}
			if (obj[1] != null) { // 总金额
				driverInfoDTO.setTotalMoney(Double.valueOf(obj[1].toString()));
			}
			if (obj[2] != null) { // 车主昵称
				driverInfoDTO.setNickName(obj[2].toString());
			}
			if (obj[3] != null) { // 车主头像
				driverInfoDTO.setUserAvatar(ToolUtil.imageAddress(obj[3].toString()));
			}
			if (obj[4] != null) { // 车辆品牌
				driverInfoDTO.setVehicleModel(obj[4].toString());
			}
			if (obj[5] != null) { // 车辆颜色
				driverInfoDTO.setVehicleColor(obj[5].toString());
			}
			if (obj[6] != null) { // 车牌号
				driverInfoDTO.setLisencePlate(obj[6].toString());
			}
			if (obj[7] != null) { // 车主信誉等级
				driverInfoDTO.setCreditLevel(Float.parseFloat(obj[7].toString()));
			}
			return driverInfoDTO;
			
		}

		return null;
	}

	/**
	 * 
	 * @Title: getUserInfoByCancelOrder 
	 * @Description: 获取取消订单的用户信息
	 * @param userId 用户Id
	 * @author YHQ 
	 * @return ResUserInfoByCancelOrderDTO    返回类型
	 * @since V2.5.6
	 * 
	 */
	@Override
	@Transactional(readOnly=true)
	public ResUserInfoByCancelOrderDTO getUserInfoByCancelOrder(String userId) {
		
		try {
			
			// 根据用户Id获取用户信息(取消订单专用)
			List<ResUserInfoByCancelOrderDTO> resUserInfoByCancelOrderDTOs = sqlBaseDao.listSqlToCustomBean(ResUserInfoByCancelOrderDTO.class, UserInfoSQL.GET_USER_INFO_BY_CANCEL_ORDER, userId); 
			
			// 判断该用户信息是否为空
			if(resUserInfoByCancelOrderDTOs != null && resUserInfoByCancelOrderDTOs.size() > 0){
				
				return resUserInfoByCancelOrderDTOs.get(0);
			}
		} catch (Exception e) {
			log.error("根据用户Id获取用户信息(取消订单专用)时出现异常，类：" + this.getClass() + " getUserInfoByCancelOrder()", e);
		}
		
		return null;
	}
}
